package com.example.fw;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;



import com.example.tests.ContactData;
public class ContactHelper extends HelperBase {

	public ContactHelper(ApplicationManager manager) {
		super(manager);
		
	}


	
	
	public void initContactCreation() {
		click(By.linkText("add new"));
		
	}

	

	public void fillContactCreation(ContactData contact) {
	 type(By.name("firstname"), contact.firstname);
	 type(By.name("lastname"), contact.lastname);
	 type(By.name("address"), contact.adress);
	 type(By.name("home"), contact.home);
	 type(By.name("mobile"), contact.mobile);
	 type(By.name("work"), contact.work);
	 type(By.name("email"), contact.email);
	 type(By.name("email2"), contact.email2);
	 selectByValue(By.name("bday"), contact.bday);
	 selectByValue(By.name("bmonth"), contact.bmonth);
	 type(By.name("byear"), contact.byear);
	 //selectByVisibleText(By.name("new_group"), contact.group);
	}

	
	public void submitContactCreation() {
		click(By.name("submit"));
	}
	
	
	public void returnToContactPage() {
		click(By.linkText("home page"));
	}



	public void initContactModification(int index) {
		List<WebElement> rows = driver.findElements (By.xpath("//tr[@name='entry']"));
		  for(WebElement row: rows) {
		  List<WebElement>cells = row.findElements (By.tagName("td"));
		  cells.get(index).findElement(By.tagName("a")).click();
	      break;
		  }
	}
		    

	
	
	public void deleteContact(){
		
		click(By.xpath("//input [@value ='Delete']"));
		
	
				
	}

	public void updateContact(){
		click(By.name("update"));
				
	}




	public List<ContactData> getContacts() {
		List<ContactData> contacts = new ArrayList<ContactData>();
		List<WebElement>checkboxes = driver.findElements(By.name("selected[]"));
		for (WebElement checkbox : checkboxes) {
		ContactData contact = new ContactData();
		String title = checkbox.getAttribute("title");
		contact.firstname = title.substring("Select (".length(), title.length() - ")".length());
		//contact.firstname = title.substring("Select (".length(), title.length() - ")".length());
		contacts.add(contact);
		}
		return contacts;
	}
	}


	
	
	
	
	

	

